package com.bys.admin.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bys.admin.system.entity.SysFile;
import com.bys.response.R;
import com.bys.admin.system.service.ISysFileService;
import com.bys.vo.PageVo;


import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bys
 * @since 2025-01-24
 */
@Tag(name = "文件接口")
@RestController
@RequestMapping("/system/sys-file")
public class SysFileController {
    @Resource
    private ISysFileService sysFileService;
    @Operation(summary="上传文件")
    @PostMapping("/upload/file")
    @Transactional(rollbackFor = Exception.class)
    public R<String> uploadFile(MultipartFile file) throws IOException {
        String url = sysFileService.uploadFile(file.getInputStream(), file.getOriginalFilename());
        SysFile sysFile = SysFile.builder().fileName(file.getOriginalFilename()).fileSize(file.getSize()).fileType(file.getContentType()).filePath(url).build();
        sysFileService.save(sysFile);
        return R.success(url);

    }

    @Operation(summary="分页查询文件")
    @GetMapping("/page")
    public R<Page<SysFile>> page(PageVo pageVo){
        Page<SysFile> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<SysFile> pageData = sysFileService.page(page);
        return R.success(pageData);
    }

}
